#! /bin/bash
. /opt/module/rebate-report/configration/config.sh

url=${BIGDATA_URL}
username=${BIGDATA_USERNAME}
password=${BIGDATA_PASSWORD}

adb_url=${BIADB_URL}
adb_username=${BIADB_USERNAME}
adb_password=${BIADB_PASSWORD}

if [ -n "$2" ] ;then do_date=$2
else
do_date=`date -d '-1 day' +%F`
fi

#按天写入分区
collect_data() {
sqoop import -D mapred.job.queue.name=root.tianyan  \
--connect "$1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false" \
--username $2 \
--password $3 \
--target-dir /warehouse/dw/other/ods/$2/$do_date  \
--delete-target-dir \
--query "$5 and  \$CONDITIONS" \
--num-mappers 1 \
--hive-drop-import-delims \
--fields-terminated-by '\001' \
--compress \
--compression-codec snappy \
--hive-import \
--hive-database dw \
--hive-table $4 \
--hive-overwrite \
--hive-partition-key dt \
--hive-partition-value $do_date \
--null-string '\\N' \
--null-non-string '\\N'
}

#写入没分区表
collect_data_np() {
sqoop import -D mapred.job.queue.name=root.tianyan  \
--connect "$1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false" \
--username $2 \
--password $3 \
--target-dir /warehouse/dw/dwd/$2  \
--delete-target-dir \
--query "$5 and  \$CONDITIONS" \
--num-mappers 1 \
--hive-drop-import-delims \
--fields-terminated-by '\001' \
--compress \
--compression-codec snappy \
--hive-import \
--hive-database dw \
--hive-table $4 \
--hive-overwrite \
--null-string '\\N' \
--null-non-string '\\N'
}

collect_ods_rebate_record(){
collect_data $url $username $password ods_rebate_record "select id
,business_type
,group_id
,company_id
,rebate_period_type
,trade_type
,consume_type
,amount
,actual_amount
,rebate_amount
,actual_rebate_amount
,source_id
,cache_key
,discount_id
,period_id
,rule_name
,discount_type
,discount_policy
,update_flag
,period_end_flag
,recharge_end_flag
,create_time
,update_time
from rebate_record where date_format(create_time,'%Y-%m-%d') <='$do_date'"
}

collect_ods_rebate_record_incr(){
collect_data $url $username $password ods_rebate_record "select id
,business_type
,group_id
,company_id
,rebate_period_type
,trade_type
,consume_type
,amount
,actual_amount
,rebate_amount
,actual_rebate_amount
,pid
,source_id
,cache_key
,discount_id
,period_id
,rule_name
,discount_type
,discount_policy
,update_flag
,period_end_flag
,recharge_end_flag
,create_time
,update_time
from rebate_record
where date_format(create_time,'%Y-%m-%d') >='$do_date'"
}

collect_ods_rebate_detail(){
collect_data $url $username $password ods_rebate_detail "select id
,recharge_id
,order_id
,release_id
,rule_id
,period_id
,business_type
,rebate_period_type
,trade_type
,consume_type
,group_id
,company_id
,station_id
,p_cart_id
,cart_id
,c_discount_money
,c_pay_money
,mc_discount_money
,mc_pay_money
,total_money
,order_create_time
,fuel_no
,quantity
,related_amt
,rebate_type
,rebate_money
,create_time
,update_time
from rebate_detail where date_format(create_time,'%Y-%m-%d') <='$do_date'"
}

collect_ods_rebate_detail_incr(){
collect_data $url $username $password ods_rebate_detail "select id
,recharge_id
,order_id
,release_id
,rule_id
,period_id
,business_type
,rebate_period_type
,trade_type
,consume_type
,group_id
,company_id
,station_id
,p_cart_id
,cart_id
,c_discount_money
,c_pay_money
,mc_discount_money
,mc_pay_money
,total_money
,order_create_time
,fuel_no
,quantity
,related_amt
,rebate_type
,rebate_money
,create_time
,update_time
from rebate_detail
where date_format(create_time,'%Y-%m-%d') >='$do_date'"
}

collect_dim_profit_company(){
collect_data_np $adb_url $adb_username $adb_password dim_profit_company "select group_id
,customer_number
,identify
,group_name
,saler_id
,saler_name
,province_id
,province_name
,team_id
,team_name
,is_special_fund
from wjy_rebate_company
where 1=1"
}

case $1 in "ods_rebate_record")
collect_ods_rebate_record
;;
"ods_rebate_record_incr")
collect_ods_rebate_record_incr
;;
"ods_rebate_detail")
collect_ods_rebate_detail
;;
"ods_rebate_detail_incr")
collect_ods_rebate_detail_incr
;;
"dim_profit_company")
collect_dim_profit_company
;;

"init")
collect_ods_rebate_record
collect_ods_rebate_detail
;;

"all")
collect_ods_rebate_record_incr
collect_ods_rebate_detail_incr
collect_dim_profit_company
;;

esac